﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PictureBoard4.Shape
{
   public class ManageDraw
    {
        public bool Flag { get; set; }
        public Size BoradSize { get; set; }
        public bool MoveFlag { get; set; }
        public Point StartPorin;
        //定义画布
        private Graphics gDraw;
        //定义图片
        private Image CustomImg;
        //定义当前图形
        private Shape CurrentShape;
        //给自定义画布添加元素
        public void DrawImage(Shape p)
        {
            Graphics g = Graphics.FromImage(CustomImg);
            CurrentShape = null;
            p.Draw(g);
            g.Dispose();
        }
       //绘制边框
        public void DrawBoard(Graphics g)
        {
            DrawTool.DrawBoard(CurrentShape, g);

        }
        public ManageDraw(Graphics g, Size size)
        {
            gDraw = g;
            BoradSize = size;
            CustomImg = new Bitmap(BoradSize.Width, BoradSize.Height);

        }
       //开始
        public bool Start(Point p)
        {
            if (CurrentShape != null)
            {
                bool f = DrawTool.IsBound(CurrentShape, p);
                if (f)
                {
                    MoveFlag = true;
                    Flag = true;
                    StartPorin = p;
                    return true;
                }
                else
                {
                    DrawImage(CurrentShape);
                }
            }
            Flag = true;
            CurrentShape = FactoryShape.CreateShape();
            CurrentShape.Add(p);
            return false;
        }
        //处理
        public void Process(Point p)
        {
            if (CurrentShape != null && Flag)
            {

                if (MoveFlag)
                {
                    StartPorin.X = p.X - StartPorin.X;
                    StartPorin.Y = p.Y - StartPorin.Y;
                    CurrentShape.Move(StartPorin);
                    StartPorin = p;
                }
                else
                {
                    CurrentShape.Isdraw = true;
                    CurrentShape.Add(p);
                }
                Draw();
            }
        }
       //结束
        public void End(Point p)
        {
            if (CurrentShape != null)
            {
                if (CurrentShape.Isdraw)
                {

                    if (CurrentShape is CarveImage && !MoveFlag)
                    {
                        CarveImage image = CurrentShape as CarveImage;
                        image.SetImage(CustomImg);
                        DrawTool.BackColor(CurrentShape, Graphics.FromImage(CustomImg));
                    }
                    Flag = false;
                    MoveFlag = false;
                }
                else
                {
                    CurrentShape = null;
                    Draw();
                }
            }
        }
       //重绘
        public void Draw() 
        {
            if (CustomImg != null)
            {
                Bitmap map = null;
                Graphics g = null;
                map = new Bitmap(BoradSize.Width, BoradSize.Height);
                g = Graphics.FromImage(map);
                g.Clear(Tool.BackColor);
                g.DrawImage(CustomImg, new Point(0, 0));
                if (CurrentShape != null)
                {
                    if (!(CurrentShape is Pencil||CurrentShape is Eraser))
                    {
                      DrawBoard(g);
                    }
                  
                    CurrentShape.Draw(g);
                }
                gDraw.DrawImage(map,new Point(0,0));
                map.Dispose();
                g.Dispose();
            }
        }
       //读取背景图片
        public bool Read(string Path)
        {
            try
            {
                CustomImg = Bitmap.FromFile(Path);
                BoradSize = CustomImg.Size;
                return true;
            }
            catch
            {
                return false;
            }
        }
       //保存图片
        public bool Save(string Path) 
        {
            if(CustomImg!=null)
            {
              CustomImg.Save(Path);
                return true;
            }
            return false;
        }
       //得到图片
        public void GetImage(Image image) {
            CustomImg = image;
        }
    }
}
